<?php
namespace app\admin\controller;

use app\common\model\JingshuProgress as JingshuProgressModel;
use app\common\model\Jingshu as JingshuModel;

/**
 * 经书抄写进度控制器
 * Class Jingshuprogress
 * @package app\index\controller
 */
class Jingshuprogress extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new JingshuProgressModel;
        $jmodel = new JingshuModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            if (!empty($param['con'])) {
                $map['m.username|m.tel|c.id'] = ['like', "%" . $param['con'] . "%"];
            }
            if (!empty($param['start']) && !empty($param['end'])) {
                $map['c.create_time'] = ['between', [strtotime($param['start']), strtotime($param['end'])]];
            }
            $map['c.status'] = ['neq', 3];
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlists($map, $this->from, $this->size, 'c.id,c.jid,c.status,c.create_time,c.update_time,c.number,m.username,m.img,m.tel');
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $jingshu = $jmodel->findData($v['jid'],'id','title,number');
                $v['jingshu'] = $jingshu['title'];
                $v['progress'] = round($v['number'] / $jingshu['number'],4) * 100 .'%';
            }
        } catch (\Exception $e) {
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}